package miiteen.customsportal.film.service;

import miiteen.customsportal.basic.beans.BasicOverviewImage;
import miiteen.customsportal.film.beans.FilmResource;
import miiteen.customsportal.film.dao.FilmResourceDao;
import miiteen.customsportal.html.beans.HtmlContent;
import miiteen.customsportal.html.dao.HtmlContentDao;
import miiteen.hr.pubconfig.CurrentHrInfo;
import miiteen.hr.user.beans.UserUserInfo;
import miiteen.sys.login.bean.UserShiro;
import miiteen.sys.login.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import miiteen.data.dao.BaseDao;
import miiteen.data.service.impl.BaseServiceImpl;

import miiteen.customsportal.film.beans.FilmInfo;
import miiteen.customsportal.film.dao.FilmInfoDao;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Name: CustomsPortal_Film_Info
 * Des: -Service
 * Author: yulp
 * Date: 2020-07-03
 */
@Service("filmInfoService")
public class FilmInfoService extends BaseServiceImpl<FilmInfo> {

    @Autowired
    private FilmInfoDao filmInfoDao;

    @Override
    protected BaseDao<FilmInfo> getDao() {
        return filmInfoDao;
    }

    @Autowired
    private FilmResourceDao filmResourceDao;

    @Autowired
    private HtmlContentDao htmlContentDao;

    @Transactional
    public long save(FilmInfo filmInfo){
        UserShiro.ShiroUser shiroUser= LoginUtil.getCurUser();
        UserUserInfo userInfo = CurrentHrInfo.getUserInfo(shiroUser);
        boolean isUpdate=filmInfo.getId()>0;
        if(filmInfo.getId()>0){
            //修改
            filmInfo.setUpdateId(userInfo.getId());
            filmInfo.setUpdateTime(new Date());
            filmInfo.setUpdator(userInfo.getTrueName());

            //保存
        }else {
            filmInfo.setCreateId(userInfo.getId());
            filmInfo.setCreateTime(new Date());
            filmInfo.setCreator(userInfo.getTrueName());
        }

        long result=filmInfoDao.save(filmInfo);

        if(result>0){
            //删除原先子表的数据
            long number=filmResourceDao.delete(String.valueOf(filmInfo.getId()));

            for(FilmResource filmResource:filmInfo.getFileResourceList()){
                filmResource.setFileId(filmInfo.getId());
                if(filmResourceDao.save(filmResource)<0){
                    throw  new RuntimeException("保存失败！");
                }
            }
            HtmlContent htmlContent=new HtmlContent();
            htmlContent.setInfoId(filmInfo.getId());
            htmlContent.setHtmlContent(filmInfo.getHtmlContent());
            htmlContent.setType(1);
            Map<String,Object> map=new HashMap<>();
            map.put("infoId",filmInfo.getId());
            List<HtmlContent> list= htmlContentDao.findListByMap(map);
            if(isUpdate){
                if(list.size()>0){
                    htmlContent.setId(list.get(0).getId());
                }
            }
                htmlContentDao.save(htmlContent);
        }

        return result;
    }

    @Transactional
    public int delete(String ids){
        //删除主表数据
        int result=filmInfoDao.delete(ids);

        //删除子表数据
        filmResourceDao.delete(ids);

        return result;

    }

    @Transactional
    public List<FilmInfo> findByMapPreviousNext(Map<String, Object> map){
        return filmInfoDao.findByMapPreviousNext(map);
    }

    @Transactional
    public   List<FilmInfo>    getpages(Map<String, Object> conditionMap){
        return filmInfoDao.getpages(conditionMap);
    }


}
